﻿package controllers.myinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.catalina.User;

import models.Stzd;
import models.Sysinfo;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import rbac.models.Oplog;
import rbac.models.User;
import utils.NumberUtil;
import utils.PinyingUtil;

import controllers.ApplicationController;


public class StzdController extends ApplicationController {
	
	public void index() throws Exception{
		String passmode = "";
		if (isAdmin()) passmode = "true";
		request.setAttribute("passmode", passmode);	
	}
	
	public View getStzdItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		User user = (User) rbac.getCurrentUser();
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		
		if (q != null){
			cond += " and (name like '%"+q+"%' or memo like '%"+q+"%' ) ";			
		}
		**/
		if (!isAdmin())
			cond += "z_id = '"+ user.getDepartment().getCompany_id()+"'";

		Object[] args = tmpArgs.toArray();
		
		long total = Stzd.count(Stzd.class, cond, args);
		List<Stzd> items = Stzd.findAll(Stzd.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View saveStzd(Stzd item, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		//其他需要系统自动填写的字段
		//其他操作如写入日志
		item.setPy(PinyingUtil.getHeadByStrings(item.getDz()));
		item.setFullpy(PinyingUtil.hanziToPinyin(item.getDz()));
		item.setPassflag(-1);
		User user = (User) rbac.getCurrentUser();
		item.setOper(user.getLogin());
		item.setInputdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setZ_id(user.getDepartment().getCompany_id());
		item.setUnitname(user.getDepartment().getSxname());
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新增楼盘字典，内容为：" + new JsonView(item));									

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View updateStzd(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		Stzd item = Stzd.find(Stzd.class, id);
		JsonView olditem= new JsonView(item);
		item = Stzd.updateModel(item, params);
    //其他需要系统自动填写的字段
    //其他操作如写入日志
		item.setPy(PinyingUtil.getHeadByStrings(item.getDz()));
		item.setFullpy(PinyingUtil.hanziToPinyin(item.getDz()));
		item.setPassflag(-1);
		User user = (User) rbac.getCurrentUser();
		item.setOper(user.getLogin());
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setZ_id(user.getDepartment().getCompany_id());
		item.setUnitname(user.getDepartment().getSxname());
		if ((isAdmin()) || (item.getOper().equals(user.getLogin())))
		{
			if (item.save()){
				Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改楼盘字典，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));									

				result.put("success", true);
			} else {
				result.put("failure", true);
				result.put("msg", getErrorMsg(item));
			}
		}else{
			result.put("failure", true);
			result.put("msg", "你没有权限操作这条记录。");			
		}
		
		
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View destroyStzd(int id) throws Exception {
		Stzd item = Stzd.find(Stzd.class, id);
		User user = (User) rbac.getCurrentUser();
		if ((isAdmin()) || (item.getOper().equals(user.getLogin())))
		{
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除楼盘字典，内容为：" + new JsonView(item));									

		item.destroy();
		}
		return new JsonView("success:true");
	}
	
	public View batpassStzd(String ids) throws Exception {
		if ((isAdmin()) && (ids!=null) && (NumberUtil.isNumbers(ids)))
		{
			User user = (User) rbac.getCurrentUser();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")批量通过楼盘字典，编号为：" + ids);									

			Stzd.execute("update Stzd set  passflag = 0 where id in ("+ids+")", null);
		}
		return new JsonView("success:true");
	}
	
	public View batunpassStzd(String ids) throws Exception {
		if ((isAdmin()) && (ids!=null) && (NumberUtil.isNumbers(ids)))
		{
			User user = (User) rbac.getCurrentUser();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")批量取消通过楼盘字典，编号为：" + ids);									

			Stzd.execute("update Stzd set  passflag = -1 where id in ("+ids+")", null);
		}
		return new JsonView("success:true");
	}
	
	public View passStzd(int id) throws Exception {
		if (isAdmin())
		{
		Stzd item = Stzd.find(Stzd.class, id);
		item.setPassflag(0);
		item.save();
		User user = (User) rbac.getCurrentUser();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")通过楼盘字典，内容为：" + new JsonView(item));									

		}
		return new JsonView("success:true");
	}
	
	public View unpassStzd(int id) throws Exception {
		if (isAdmin())
		{
		Stzd item = Stzd.find(Stzd.class, id);
		item.setPassflag(-1);
		item.save();
		User user = (User) rbac.getCurrentUser();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")取消通过楼盘字典，内容为：" + new JsonView(item));									

		}
		return new JsonView("success:true");
	}
	
}
